(doc, docx)文档合并的三种方法 | 您所在的位置:网站首页 › dos 合并两个doc文档 › (doc, docx)文档合并的三种方法 |
Word文档合并几种方式
通过com.spire.doc包
具体参考地址:https://www.e-iceblue.cn/spiredocforjavaoperating/merge-word-documents-in-java.html,它是自己开发的一个东西,所以在maven仓库中你是搜索不到这个包的,所以引入的时候需要将包和仓库一起引入进来,不然编译的时候他会报com.spire.doc这个包找不到什么的。http://repo.e-iceblue.cn/repository/maven-public/这个是它的仓库,你可以直接点进去看看他还有哪些包。具体的请看他们的官网文档,感觉蛮好。 e-iceblue spire.doc 3.4.10
com.e-iceblue http://repo.e-iceblue.cn/repository/maven-public/ 文档合并,第二个文档不填充到第一个文档的末尾 // 因里面的源码被对面防止反编译了,所以对其源码的解析就不深入的探讨,他只是对外开放的提供了几个较好的方法。 public class MergeWordDocument { public static void main(String[] args){ //获取第一个文档的路径 String filePath1 = "merge1.docx"; //获取第二个文档的路径 String filePath2 = "merge2.docx"; //加载第一个文档 Document document = new Document(filePath1); //使用insertTextFromFile方法将第二个文档的内容插入到第一个文档 document.insertTextFromFile(filePath2, FileFormat.Docx_2013); //保存文档 document.saveToFile("Output.docx", FileFormat.Docx_2013); } } 我们可以使用Document类中的insertTextFromFile方法将不同的文档合并到同一个文档。需要注意的是,使用该方法合并文档时,被合并文档的内容默认从新的一页拼接。 弊端: 当你使用的不是免费的时候,会在你合并的文档中出现一行字 Evaluation Warning: The document was created with Spire.Doc for JAVA. 文档合并,第二个文档填充到第一个文档的末尾,section的使用,这里可以合并多个文件,传递的参数是文件路径。public static void merge1(List fileList) { List docList = new ArrayList(fileList.size()); for (String str : fileList) { Document document = new Document(str); docList.add(document); } for (int i = 0; i < docList.size(); i++) { if (i != docList.size()-1) { Section lastSection = docList.get(i).getLastSection(); for (Section section : (Iterable) docList.get(i + 1).getSections()) { for(DocumentObject obj: ( Iterable ) section.getBody().getChildObjects()) { lastSection.getBody().getChildObjects().add(obj.deepClone()); } } docList.get(i).saveToFile("Doc1.doc", FileFormat.Docx_2013); } } } 通过POI的形式进行文档合并针对于POI的形式进行文档合并,主要是通过XWPFDocument对象操作的是后缀名为docx的文档和HWPFDocument 对象操作后缀名为doc文档,两者对象的区别很大。 目前就使用XWPFDocument 操作。 需要引入的jar包主要是以下几个,我写demo的使用用的springboot,所以直接在pom文件里面加依赖就行了,如果各位不是通过pom.xml文件的形式引入的话,去maven中心仓库找,下jar包。 引入jar包org.apache.poi poi 3.8 org.apache.poi poi-scratchpad 3.8 org.apache.poi poi-ooxml 3.8 上代码public static void mergeWord(List wordList, OutputStream outputStream) throws IOException, XmlException { if (CollectionUtils.isEmpty(wordList)) { return; } // docx XWPFDocument newDocument = null; CTBody newCtBody = null; Map headMap = new HashMap(30); String newString = null; String prefix = null; StringBuilder mainPart = new StringBuilder(); for (int i = 0; i < wordList.size(); ++i) { try (InputStream word = wordList.get(i)) { XWPFDocument xwpfDocument = new XWPFDocument(word); if (i != wordList.size() - 1) { XWPFRun run = xwpfDocument.getLastParagraph().createRun(); run.addBreak(BreakType.PAGE); } CTBody ctBody = xwpfDocument.getDocument().getBody(); if (i == 0) { newDocument = xwpfDocument; newCtBody = ctBody; newString = newCtBody.xmlText(); prefix = newString.substring(0, newString.indexOf('>') + 1); mainPart.append(newString, newString.indexOf('>') + 1, newString.lastIndexOf(' |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |